<?php

namespace App\Http\Controllers\Shop\Business;

use App\Http\Controllers\ShopController;
use App\Models\Business\Address as AddressModel;
use App\Models\Region as RegionModel;
use Illuminate\Database\QueryException;
use Illuminate\Support\Facades\Validator;

class AddressController extends ShopController
{
    public function index()
    {
        $busid = request('busid',0);

        $list = AddressModel::where(['busid' => $busid])->orderBy('id','desc')->get();

        if($list)
        {
            return $this->success('查询成功',$list);
        }else{
            return $this->error('暂无数据',[]);
        }
    }

    public function add()
    {
        $params = request()->input();

        $data = [
            'busid' => $params['busid'],
            'consignee' => trim($params['consignee']),
            'mobile' => trim($params['mobile']),
            'address' => trim($params['address']),
            'status' => (String)$params['status']
        ];

        // 把该用户所有的收货地址全部给设置回未默认状态
        if($params['status'] === 1)
        {
            $res = AddressModel::where('busid',$params['busid'])->update(['status' => '0']);

            if($res === false)
            {
                return $this->error('更新该用户默认收货地址失败');
            }
        }

        // 地区
        $path = RegionModel::where('code',$params['code'])->value('parentpath');

        if(!$path)
        {
            return $this->error('所选地区不存在');
        }

        [$province,$city,$district] = explode(',',$path);

        $data['province'] = $province ?? null;
        $data['city'] = $city ?? null;
        $data['district'] = $district ?? null;

        $validate = [
            [
                'consignee' => 'required', //必填
                'mobile' => 'required', //必填
                'address' => 'required', //必填
                'status' => 'in:0,1',  //给字段设置范围
                'busid' => 'required', //必填
            ],
            [
                'consignee.required' => '请输入收货人名称',
                'mobile.required' => '请输入手机号码',
                'address.required' => '请输入详细地址',
                'busid.required' => '用户信息未知',
                'status.in' => '默认收货地址未知'
            ]
        ];

        $validator = Validator::make($data,...$validate);

        if($validator->fails())
        {
            return $this->error($validator->errors()->first());
        }

        $result = AddressModel::create($data);

        if($result === false)
        {
            return $this->error('新增收货地址失败');
        }else{
            return $this->success('新增收货地址成功');
        }
    }

    public function selected()
    {
        $busid = request()->input('busid',0);
        $id = request()->input('id',0);

        try {
            
            $address = AddressModel::where(['busid' => $busid,'id' => $id])->first();

            if(!$address)
            {
                throw new \Exception('收货地址不存在');
            }

            $res = AddressModel::where(['busid' => $busid])->update(['status' => '0']);

            if($res === false)
            {
                throw new \Exception('更新用户默认收货地址失败');
            }

            $address->status = 1;

            $result = $address->save();

            if($result === false)
            {
                throw new \Exception('更新默认收货地址失败');
            }
        
        } catch (\Exception|QueryException $e) {
            return $this->error($e->getMessage());
        }

        return $this->success('更新默认收货地址成功');
    }

    public function info()
    {
        $busid = request('busid',0);
        $id = request('id',0);

        $address = AddressModel::where(['id' => $id,'busid' => $busid])->first();

        if($address)
        {
            return $this->success('查询成功',$address);
        }else{
            return $this->error('该收货地址不存在');
        }
    }

    public function edit()
    {

    }

    public function del()
    {
        $busid = request('busid',0);
        $id = request('id',0);

        $address = AddressModel::where(['id' => $id,'busid' => $busid])->first();

        if(!$address)
        {
            return $this->error('该收货地址不存在');
        }

        $result = AddressModel::destroy($id);

        if($result === false)
        {
            return $this->error('删除失败');
        }else{
            return $this->success('删除成功');
        }
    }
}
